/**
 * <br>项目名: fastdfs-api
 * <br>文件名: UploadCtrl.java
 * <br>Copyright 2015 恒昌互联网运营中心
 */
package com.credithc.fastdfs.api.web;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.credithc.fastdfs.api.bean.ResultBean;
import com.credithc.fastdfs.api.bean.SearchReqBean;
import com.credithc.fastdfs.api.bean.SearchResBean;
import com.credithc.fastdfs.api.config.SystemConfig;
import com.credithc.fastdfs.api.service.FdfsInfoService;
import com.credithc.fastdfs.api.util.Validation;
import com.credithc.framework.core.mybatis.page.PageController;
import com.credithc.framework.core.utils.FastJsonConvert;

/**
 * @ClassName: SearchCtrl
 * @Description: 通过系统ID、业务ID查询文件列表
 * @author zhaoc
 * @date 2015年5月27日
 * @time 上午10:11:13
 */
@Controller
@RequestMapping("/searchFile.hc")
public class SearchCtrl extends BaseController {
	private Logger log = LoggerFactory.getLogger(SearchCtrl.class);
	public static final String SUCCESS = "1";// 上传成功
	public static final String FAILED = "0";// 上传失败

	@Resource(name = "fdfsInfoServiceImpl")
	private FdfsInfoService fdfsInfoServiceImpl;

	/**
	 * @Title: search
	 * @Description: 文件查询
	 * @param
	 * @return String
	 * @throws
	 */
	@RequestMapping(method = { RequestMethod.POST })
	public String search(SearchReqBean searchReqBean, HttpServletResponse response) throws IOException {
		SearchResBean searchResBean = new SearchResBean();
		ResultBean resultBean = new ResultBean();
		resultBean.setResult("1");
		resultBean.setErrorMsg("");
		try {
			if (handler(searchReqBean, searchResBean)) { // 文件删除操作成功
				searchResBean.setStatus(RemoveCtrl.SUCCESS);
				searchResBean.setMessage("查询成功");
			}
		} catch (Exception ex) {
			searchResBean.setStatus(RemoveCtrl.FAILED);
			searchResBean.setMessage(ex.getMessage());
			log.warn(ex.getMessage());
			addError(ex.getMessage(), MOD_SEARCH, "/");

		}
		resultBean.setData(searchResBean);
		return ajaxHtml(FastJsonConvert.convertObjectToJSON(resultBean),
				response);
	}

	/**
	 * @Title: handler
	 * @Description: 文件状态修改操作
	 * @param
	 * @return boolean
	 * @throws
	 */
	public boolean handler(SearchReqBean searchReqBean,
			SearchResBean searchResBean) throws Exception {
		if (StringUtils.isBlank(searchReqBean.getSystemId())) {//校验系统ID
			searchResBean.setStatus(SearchCtrl.FAILED);
			searchResBean.setMessage("系统ID不合法");
			return false;
		}
		if (StringUtils.isBlank(searchReqBean.getBusinessId())) {//校验业务ID
			searchResBean.setStatus(SearchCtrl.FAILED);
			searchResBean.setMessage("业务ID不合法");
			return false;
		}
		int currentPage = 1;
		int pageSize = SystemConfig.DEFAULT_FILE_COUNT; //每页默认显示数量
		if(StringUtils.isNotBlank(searchReqBean.getCurrentPage())) {//页码不为空
			if(!Validation.validate(searchReqBean.getCurrentPage(), Validation.INT) || Integer.valueOf(searchReqBean.getCurrentPage()) <= 0) { //校验页码是否为数字
				searchResBean.setStatus(SearchCtrl.FAILED);
				searchResBean.setMessage("页码不合法");
				return false;
			} 
			currentPage = Integer.valueOf(searchReqBean.getCurrentPage());
			searchResBean.setCurrentPage(currentPage);
		}
		if(StringUtils.isNotBlank(searchReqBean.getPageSize())) { //每页显示数量不为空
			if(!Validation.validate(searchReqBean.getPageSize(), Validation.INT) || Integer.valueOf(searchReqBean.getPageSize()) > SystemConfig.MAX_FILE_COUNT) {//校验每页最大查询数量
				searchResBean.setStatus(SearchCtrl.FAILED);
				searchResBean.setMessage("每页显示数量不合法,应为小于"
						+ SystemConfig.MAX_FILE_COUNT + "的正整数");
				return false;
			}
			pageSize = Integer.valueOf(searchReqBean.getPageSize());
			searchResBean.setPageSize(pageSize);
		}
		//查询满足条件文件数量
		PageController p = new PageController(currentPage, pageSize);
		int count = fdfsInfoServiceImpl.selectFsIdCountBySystemIdAndBusinessId(searchReqBean.getSystemId(), searchReqBean.getBusinessId());
		List<String> fsList = fdfsInfoServiceImpl.selectFsIdBySystemIdAndBusinessId(p, searchReqBean.getSystemId(), searchReqBean.getBusinessId());
		searchResBean.setFsList(fsList);
		searchResBean.setTotalCount(count);
		return true;
	}

}
